package cn.stronglink.assetmanage.business.check.add;

import android.content.Intent;
import android.database.CursorIndexOutOfBoundsException;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.stronglink.assetmanage.BaseActivity;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.assetlist.CheckAssetAdapter;
import cn.stronglink.assetmanage.business.check.detail.AddCheckDetailActivity;
import cn.stronglink.assetmanage.business.check.detail.CheckDetailActivity;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.listener.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.dao.CheckDataUtil;

/**
 * 添加新的入库单
 */
public class AddNewCheckActivity extends BaseActivity implements onRecyclerListener, View.OnClickListener {
    private CheckBox cbCheck;
    private RecyclerView rvAddCheckAssetList;
    private List<Asset> mAddAssetList = new ArrayList<>();
    private CheckAssetAdapter mAdapter;
    private String orderNo, createTime;
    private TextView tvAddOrderNo;
    private Button btnAddCheck, btnCanclCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_check);
        initDataAndView();
    }

    private void initDataAndView() {
        initView();
        initData();
    }


    private void initView() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        //2018-06-07 15:23:58
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currentTime = new Date().getTime();
        orderNo = sdf.format(currentTime);
        createTime = sdfDate.format(currentTime);
        tvAddOrderNo = findViewById(R.id.tv_add_order_no);
        tvAddOrderNo.setText(orderNo);
        cbCheck = findViewById(R.id.cb_check);
        cbCheck.setVisibility(View.GONE);
        rvAddCheckAssetList = findViewById(R.id.rv_add_check_asset_list);
        btnAddCheck = findViewById(R.id.btn_add_asset);
        btnCanclCheck = findViewById(R.id.btn_add_order_cancel);
        btnAddCheck.setOnClickListener(this);
        btnCanclCheck.setOnClickListener(this);
    }

    private void initData() {
        mAdapter = new CheckAssetAdapter(this);
        mAdapter.setListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        rvAddCheckAssetList.setLayoutManager(layoutManager);
        rvAddCheckAssetList.setAdapter(mAdapter);
        mAddAssetList = CheckDataUtil.getAddCheckAssetByOrderNo(orderNo);
        mAdapter.notifyDataChanged(mAddAssetList);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setSelectPosition(1);
        initData();
        mAdapter.setListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.btn_add_asset:
                //跳转到新加资产页
                try {
                    Check check = CheckDataUtil.getCheckByOrderNo(orderNo);
                    if (check == null) {
                        check = new Check();
                        check.setId_(orderNo);
                        check.setCreateTime(createTime);
                        check.setCreateBy(Long.valueOf(User.getInstance().getId_()));
                        check.setUpdateBy(Long.valueOf(User.getInstance().getId_()));
                        check.setUpdateTime(createTime);
                        check.setNewAdd(true);
                        check.setCreateUser(User.getInstance().getName());
                        CheckDataUtil.addCheckedOrder(check);
                    }
                    Intent intent = new Intent(AddNewCheckActivity.this,
                            AddCheckDetailActivity.class);
                    intent.putExtra(Constants.CHECK_IN_DETAIL_NO, orderNo);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_add_order_cancel:
                //取消本单
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClicked(View v, int postion) {
        // 跳转到详情页
        if (mAddAssetList != null && mAddAssetList.size() - 1 >= postion) {
            Intent intent = new Intent(this, CheckDetailActivity.class);
            intent.putExtra(Constants.CHECK_IN_DETAIL_NO, mAddAssetList.get(postion).getAsset_id_key());
            startActivity(intent);
        }
    }

    @Override
    protected void setSelectPosition(int position) {
        super.setSelectPosition(position);
        tvBusinessName.setText(getString(R.string.check_add_new_order_title));
    }
}
